package com.zw.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.zw.common.entity.product.AttrEntity;
import com.zw.common.entity.product.AttrGroupEntity;
import com.zw.gulimall.product.service.CategoryService;
import com.zw.gulimall.product.vo.AttrGroupRelationVo;
import com.zw.gulimall.product.vo.AttrGroupWithAttrRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zw.gulimall.product.service.AttrGroupService;
import com.zw.common.utils.PageUtils;
import com.zw.common.utils.R;


/**
 * 属性分组
 *
 * @author zw
 * @email Alisinngai@gmail.com
 * @date 2023-06-11 23:54:03
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


//    /product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrGroupRelation(@PathVariable("attrgroupId") Long attrGroupId){
        List<AttrEntity> attrGroupEntityList = attrGroupService.queryAttrGroupRelation(attrGroupId);
        return  R.ok().put("data",attrGroupEntityList);
    }

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noAttrGroupRelation(Map<String, Object> params,@PathVariable("attrgroupId") Long attrGroupId){
        PageUtils page = attrGroupService.queryNoAttrGroupRelationPage(params,attrGroupId);
        return  R.ok().put("page",page);
    }
//    /product/attrgroup/attr/relation

    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrGroupRelationVo> vos){
        attrGroupService.saveAttrRelation(vos);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] catelogPath = categoryService.findCatlogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

//    /product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R attrGroupWithAttr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrRespVo> attrResps = attrGroupService.queryAttrGroupWithAttrByCatelogId(catelogId);
        return R.ok().put("data",attrResps);
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

//    /product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    private R deleteRelation(@RequestBody List<AttrGroupRelationVo> vo){
        attrGroupService.deleteRelation(vo);
        return R.ok();
    }
}
